package no.nordicsemi.android.nrfmesh.widgets;

import android.util.Log;
import no.nordicsemi.android.mesh.transport.MeshMessage;
import no.nordicsemi.android.mesh.transport.VendorModelMessageStatus;

/**
 * 消息监控工具 - 监控所有接收到的Mesh消息
 */
public class MessageMonitor {
    private static final String TAG = "MessageMonitor";
    private static boolean isMonitoring = false;
    private static int totalMessageCount = 0;
    private static int vendorMessageCount = 0;
    
    /**
     * 开始消息监控
     */
    public static void startMonitoring() {
        isMonitoring = true;
        totalMessageCount = 0;
        vendorMessageCount = 0;
        Log.d(TAG, "🔍 开始监控所有Mesh消息");
        Log.d(TAG, "=================================");
    }
    
    /**
     * 停止消息监控
     */
    public static void stopMonitoring() {
        if (isMonitoring) {
            Log.d(TAG, "=================================");
            Log.d(TAG, "📊 消息监控统计:");
            Log.d(TAG, "  总消息数: " + totalMessageCount);
            Log.d(TAG, "  VendorModelMessageStatus数: " + vendorMessageCount);
            Log.d(TAG, "🔍 停止消息监控");
        }
        isMonitoring = false;
    }
    
    /**
     * 监控消息接收
     */
    public static void onMessageReceived(MeshMessage meshMessage) {
        if (!isMonitoring) return;
        
        totalMessageCount++;
        
        Log.d(TAG, "📨 收到消息 #" + totalMessageCount);
        Log.d(TAG, "  消息类型: " + meshMessage.getClass().getSimpleName());
        
        if (meshMessage instanceof VendorModelMessageStatus) {
            vendorMessageCount++;
            VendorModelMessageStatus vendorMessage = (VendorModelMessageStatus) meshMessage;
            
            Log.d(TAG, "  ✅ VendorModelMessageStatus消息 #" + vendorMessageCount);
            Log.d(TAG, "  OpCode: 0x" + Integer.toHexString(vendorMessage.getOpCode()).toUpperCase());
            
            byte[] parameters = vendorMessage.getParameters();
            if (parameters != null) {
                Log.d(TAG, "  参数长度: " + parameters.length + " bytes");
                if (parameters.length > 0) {
                    StringBuilder hex = new StringBuilder();
                    for (byte b : parameters) {
                        hex.append(String.format("%02X ", b));
                    }
                    Log.d(TAG, "  参数内容(HEX): " + hex.toString().trim());
                    
                    // 尝试转换为字符串
                    try {
                        String paramStr = new String(parameters, "UTF-8");
                        Log.d(TAG, "  参数内容(UTF-8): " + paramStr);
                    } catch (Exception e) {
                        Log.d(TAG, "  参数内容: 无法转换为UTF-8字符串");
                    }
                }
            } else {
                Log.d(TAG, "  参数: null");
            }
        } else {
            Log.d(TAG, "  ❌ 非VendorModelMessageStatus消息");
        }
        
        Log.d(TAG, "  源地址: 0x" + Integer.toHexString(meshMessage.getSrc()).toUpperCase());
        Log.d(TAG, "  目标地址: 0x" + Integer.toHexString(meshMessage.getDst()).toUpperCase());
        Log.d(TAG, "---");
    }
    
    /**
     * 检查监控状态
     */
    public static void checkStatus() {
        Log.d(TAG, "📊 当前监控状态:");
        Log.d(TAG, "  监控中: " + (isMonitoring ? "是" : "否"));
        Log.d(TAG, "  总消息数: " + totalMessageCount);
        Log.d(TAG, "  VendorModelMessageStatus数: " + vendorMessageCount);
    }
    
    /**
     * 记录期望的OpCode信息
     */
    public static void logExpectedOpCode() {
        Log.d(TAG, "🎯 期望的消息信息:");
        Log.d(TAG, "  消息类型: VendorModelMessageStatus");
        Log.d(TAG, "  期望OpCode: 0x1");
        Log.d(TAG, "  期望参数: JSON格式的设备状态数据");
    }
}